<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="../../../../lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="../../../../lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="../../../../css/main.css?v=5.1.2" rel="stylesheet" type="text/css">


  <meta name="keywords" content="homebrew,python,pyenv,macos,">








  <link rel="shortcut icon" type="image/x-icon" href="../../../../favicon.ico?v=5.1.2">






<meta name="description" content="说明目前Python同时更新与维护Python2和Python3，选择Python2还是选择Python3，取决于当前要使用的库、框架支持哪个版本，所以经常会遇到切换版本的情况。 那么应该怎样有效的更改呢？很多小伙伴一定会想到修改环境变量，指定Python的默认路径，这样当然可以，然而不够优雅。那么怎样的方法才算优雅呢？当然是一条命令了👻。 这里通过brew安装pyenv，再用pyenv安装管理">
<meta name="keywords" content="homebrew,python,pyenv,macos">
<meta property="og:type" content="article">
<meta property="og:title" content="macOS 安装和管理多个Python版本">
<meta property="og:url" content="http://moonvsriver.com/2018/07/09/macos-manage-pythons/index.html">
<meta property="og:site_name" content="John&#39;s blog">
<meta property="og:description" content="说明目前Python同时更新与维护Python2和Python3，选择Python2还是选择Python3，取决于当前要使用的库、框架支持哪个版本，所以经常会遇到切换版本的情况。 那么应该怎样有效的更改呢？很多小伙伴一定会想到修改环境变量，指定Python的默认路径，这样当然可以，然而不够优雅。那么怎样的方法才算优雅呢？当然是一条命令了👻。 这里通过brew安装pyenv，再用pyenv安装管理">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-02-28T07:34:16.929Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="macOS 安装和管理多个Python版本">
<meta name="twitter:description" content="说明目前Python同时更新与维护Python2和Python3，选择Python2还是选择Python3，取决于当前要使用的库、框架支持哪个版本，所以经常会遇到切换版本的情况。 那么应该怎样有效的更改呢？很多小伙伴一定会想到修改环境变量，指定Python的默认路径，这样当然可以，然而不够优雅。那么怎样的方法才算优雅呢？当然是一条命令了👻。 这里通过brew安装pyenv，再用pyenv安装管理">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.2',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://moonvsriver.com/2018/07/09/macos-manage-pythons/">





  <title>macOS 安装和管理多个Python版本 | John's blog</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">John's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="http://moonvsriver.com/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="http://moonvsriver.com/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="http://moonvsriver.com/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="http://moonvsriver.com/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://moonvsriver.com">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="John">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="../../../../images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="John's blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">macOS 安装和管理多个Python版本</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-07-09T01:56:46+00:00">
                2018-07-09
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="../../../../categories/Python/" itemprop="url" rel="index">
                    <span itemprop="name">Python</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h4><p>目前Python同时更新与维护Python2和Python3，选择Python2还是选择Python3，取决于当前要使用的库、框架支持哪个版本，所以经常会遇到切换版本的情况。</p>
<p>那么应该怎样有效的更改呢？很多小伙伴一定会想到修改环境变量，指定Python的默认路径，这样当然可以，然而不够优雅。那么怎样的方法才算优雅呢？当然是一条命令了👻。</p>
<p>这里通过brew安装pyenv，再用pyenv安装管理Python。</p>
<h4 id="安装步骤"><a href="#安装步骤" class="headerlink" title="安装步骤"></a>安装步骤</h4><ul>
<li><h5 id="安装homebrew：🚀"><a href="#安装homebrew：🚀" class="headerlink" title="安装homebrew：🚀"></a>安装<a href="https://brew.sh/" target="_blank" rel="noopener">homebrew</a>：🚀</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ /usr/bin/ruby -e <span class="string">"<span class="variable">$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)</span>"</span></span><br><span class="line">$ brew -v</span><br><span class="line">Homebrew 1.6.9</span><br><span class="line">Homebrew/homebrew-core (git revision 5707e; last commit 2018-07-09)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>注：Homebrew 是macOS下非常高效的命令行软件包管理器，mac必安装工具之一。</p>
<ul>
<li><h5 id="安装pyenv：🛰"><a href="#安装pyenv：🛰" class="headerlink" title="安装pyenv：🛰"></a>安装<a href="https://github.com/pyenv/pyenv" target="_blank" rel="noopener">pyenv</a>：🛰</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ brew update</span><br><span class="line">$ brew install pyenv</span><br><span class="line">$ pyenv -v</span><br><span class="line">pyenv 1.2.5</span><br></pre></td></tr></table></figure>
</li>
<li><h5 id="安装管理多个Python："><a href="#安装管理多个Python：" class="headerlink" title="安装管理多个Python："></a>安装管理多个Python：</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv install 2.7.15</span><br><span class="line">$ pyenv install 3.7.0</span><br><span class="line">$ pyenv versions</span><br><span class="line">  system</span><br><span class="line">  2.7.15</span><br><span class="line">* 3.7.0 (<span class="built_in">set</span> by /Users/john/.pyenv/version)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>注：星号指定当前的版本</p>
<ul>
<li><h5 id="切换版本："><a href="#切换版本：" class="headerlink" title="切换版本："></a>切换版本：</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ pyenv global 2.7.15</span><br><span class="line">$ pyenv versions</span><br><span class="line">  system</span><br><span class="line">* 2.7.15 (<span class="built_in">set</span> by /Users/john/.pyenv/version)</span><br><span class="line">  3.7.0</span><br><span class="line">$ python --version</span><br><span class="line">Python 2.7.15</span><br></pre></td></tr></table></figure>
</li>
<li><h5 id="pyenv常用的命令说明："><a href="#pyenv常用的命令说明：" class="headerlink" title="pyenv常用的命令说明："></a>pyenv常用的命令说明：</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">使用方式: pyenv &lt;命令&gt; [&lt;参数&gt;]</span><br><span class="line"></span><br><span class="line">命令:</span><br><span class="line">   commands    查看所有命令</span><br><span class="line">   <span class="built_in">local</span>       设置或显示本地的Python版本</span><br><span class="line">   global      设置或显示全局Python版本</span><br><span class="line">   shell       设置或显示shell指定的Python版本</span><br><span class="line">   install     安装指定Python版本</span><br><span class="line">   uninstall   卸载指定Python版本)</span><br><span class="line">   version     显示当前的Python版本及其本地路径</span><br><span class="line">   versions    查看所有已经安装的版本</span><br><span class="line">   <span class="built_in">which</span>       显示安装路径</span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>注：使用local、global、shell，设置python版本时需要跟上参数（版本号），查看则不需要。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="../../../../tags/homebrew/" rel="tag"># homebrew</a>
          
            <a href="../../../../tags/python/" rel="tag"># python</a>
          
            <a href="../../../../tags/pyenv/" rel="tag"># pyenv</a>
          
            <a href="../../../../tags/macos/" rel="tag"># macos</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="../../../12/18/ubuntu-non-root-user-docker/" rel="prev" title="Ubuntu中以非root用户身份管理Docker">
                Ubuntu中以非root用户身份管理Docker <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
          
            <p class="site-author-name" itemprop="name">John</p>
            <p class="site-description motion-element" itemprop="description"></p>
        </div>

        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
            
              <a href="http://moonvsriver.com/archives/">
            
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="../../../../categories/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="../../../../tags/index.html">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/iplanetcn" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>GitHub</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://weibo.com/u/1917396204" target="_blank" title="Weibo">
                  
                    <i class="fa fa-fw fa-weibo"></i>Weibo</a>
              </span>
            
              <span class="links-of-author-item">
                <a href="mailto:iplanetcn@gmail.com" target="_blank" title="E-Mail">
                  
                    <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#说明"><span class="nav-number">1.</span> <span class="nav-text">说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#安装步骤"><span class="nav-number">2.</span> <span class="nav-text">安装步骤</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#安装homebrew：🚀"><span class="nav-number">2.1.</span> <span class="nav-text">安装homebrew：🚀</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#安装pyenv：🛰"><span class="nav-number">2.2.</span> <span class="nav-text">安装pyenv：🛰</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#安装管理多个Python："><span class="nav-number">2.3.</span> <span class="nav-text">安装管理多个Python：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#切换版本："><span class="nav-number">2.4.</span> <span class="nav-text">切换版本：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#pyenv常用的命令说明："><span class="nav-number">2.5.</span> <span class="nav-text">pyenv常用的命令说明：</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">
  
  &copy;  2017 &mdash; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">John</span>

  
</div>


  <div>
  <a href="http://www.miitbeian.gov.cn/">蜀ICP备17010115号-1</a> 
  </div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="../../../../lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="../../../../lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="../../../../lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="../../../../lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="../../../../lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="../../../../lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="../../../../js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="../../../../js/src/motion.js?v=5.1.2"></script>



  
  

  <script type="text/javascript" src="../../../../js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="../../../../js/src/post-details.js?v=5.1.2"></script>


  
  <script type="text/javascript" src="../../../../js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="../../../../js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="../../../../js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  








  





  

  

  

  

  

  

</body>
</html>
